<script lang="ts" setup>
import { computed, h, reactive, toRefs } from 'vue'
import { DownloadOutlined } from '@ant-design/icons-vue'
import Api from '@/api/'
import to from '@/utils/awaitTo'
import { download } from '@/utils/utils'
import { message } from 'ant-design-vue'


// 引用声明
const props = defineProps({
  row: {
    type: Object,
    default: () => {
    }
  }
})

// 引用事件
const emits = defineEmits(['callback'])

// 响应式数据
const state = reactive({
  loading: false
})

// 响应式数据解析
const { loading } = toRefs(state)

// 点击
const handleDownload = async () => {

  // 重新下载
  if (Number(props.row.downloadStatus) === 3001) {

    download(props.row.originalFilmUrl, props.row.topicTitle)

  }


  // 下载视频
  else {


    const params = {
      videoIds: [],
      voiceOrVideoOrProductionRequirement: 2
    }
    params.videoIds.push(props.row.videoId)
    const [error, data] = await to(Api.video.videoDownLoadEcho(params))
    if (!error) {
      const [urlList] = data
      if (urlList) {
        const {
          url: { originalFilmUrl }
        } = urlList
        const dothis = []
        dothis.push(props.row.videoId)
        loading.value = true
        const [error, data] = await to(Api.video.videoDownLoadStatus(dothis))
        loading.value = false
        console.log(data)
        if (!error && data === 1) {
          console.log(data)
          message.success(`状态已更新`)
          download(originalFilmUrl, props.row.topicTitle)
          setTimeout(() => {
            emits('callback', { type: 'download', originalFilmUrl, topicTitle: props.row.topicTitle })
          }, 2000)
        }
      } else {
        download(props.row.originalFilmUrl, props.row.topicTitle)
      }
    }


  }
}

// 是否有数智人
const isUrl = computed(() => {
  return props.row.originalFilmUrl
})
</script>

<template>
  <div>
    <template v-if="isUrl">
      <a-button v-if="Number(row.downloadStatus) === 3001" :icon="h(DownloadOutlined)" :loading="loading" type="primary"
                @click="handleDownload">重新下载
      </a-button>
      <a-button v-else :icon="h(DownloadOutlined)" :loading="loading" type="primary" @click="handleDownload">下载视频
      </a-button>
    </template>

    <template v-else>
      <a-button danger loading type="primary">原片合成中</a-button>
    </template>
  </div>
</template>

<style lang="less" scoped>

</style>
